<?php

namespace addons\helper\controller\wxapp\helper;

use addons\helper\controller\wxapp\Base;

class User extends Base
{

    protected $relationSearch = true;
    protected $searchFields = 'id,admin.username';
    protected $config = [
        'model'             => '用户',
        'limit'             => 10,
        'defaultSort'       => 'id',
        'defaultOrder'      => 'desc',
        'tabField'          => '',
        'searchPlaceholder' => '输入ID或用户名搜索',
        'multi'             => true,
        'sort'              => true,
        'filter'            => true,
        'search'            => true,
        'field' => [
            'user_id'     => ['用户ID', 'text', true,  false, '', true, false, false],
            'nickname'    => ['用户昵称', 'text', false,  false, '', false, false, false],
            'wxunionid'   => ['微信unionId', 'text', false,  false, '', true, false, false],
            'wxopenid'    => ['微信openId', 'text', false,  false, '', true, false, false],
            'wxnickname'  => ['微信昵称', 'text', false,  false, '', true, false, false],
            'wxgender'    => ['微信性别', 'text', false,  false, '', true, false, false],
            'wxcity'      => ['微信城市', 'text', false,  false, '', true, false, false],
            'wxprovince'  => ['微信省份', 'text', false,  false, '', true, false, false],
            'wxcountry'   => ['微信国家', 'text', false,  false, '', true, false, false],
            'wxavatarurl' => ['微信头像', 'text', false,  false, '', true, false, false],
            'logintime'   => ['登录时间', 'datetime', true,  true, 'RANGE', true, false, false],
            'createtime'  => ['创建时间', 'datetime', true, true, 'RANGE', true, false, false],
            'updatetime'  => ['更新时间', 'datetime', false, false, '', true, false, false],
            'status'      => ['状态', 'select', false, false, '', true, false, false],
        ],
        'fieldContent' => [
            'status' => [
                '1' => '启用',
                '0' => '禁用'
            ]
        ],
        'auth' => [
            'multi' => [
                'del'  => [
                    'name'        => '删除',
                    'color'       => '#e74c3c',
                    'icon'        => 'fa-trash',
                    'url'         => 'helper.user/del'
                ],
                'hidden'  => [
                    'name'        => '启用',
                    'color'       => '#f39c12',
                    'icon'        => 'fa-eye-slash',
                    'url'         => 'helper.user/multi',
                    'params'      => 'status=1'
                ],
                'normal'  => [
                    'name'        => '禁用',
                    'color'       => '#18bc9c',
                    'icon'        => 'fa-eye',
                    'url'         => 'helper.user/multi',
                    'params'      => 'status=0'
                ],
            ]
        ],
    ];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\helper\User;
        $this->config['field'] = $this->formatField();
        $this->config['pk'] = $this->model->getPk() ? $this->model->getPk() : 'id';
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with('admin')
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->with('admin')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => $v) {
                $v->hidden(['token']);
                $v->getRelation('admin')->visible(['id','nickname','username']);
            }
            $result = array('total' => $total, 'rows' => $list);

            $data = [
                'tabList'  => null,
                'dataList' => $result,
                'config'   => $this->config
            ];
            $this->success('ok', $data);
        }
    }

}
